home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 610 < prev    next >
Text File  |  1996-08-06  |  2KB  |  76 lines

  1. Path: lyra.csx.cam.ac.uk!jkb
  2. From: jkb@mrc-lmb.cam.ac.uk (James Bonfield)
  3. Newsgroups: comp.std.c
  4. Subject: Restrictions on qsort compare function?
  5. Date: 20 Mar 1996 09:56:41 GMT
  6. Organization: MRC Laboratory of Molecular Biology, Cambridge UK
  7. Distribution: world
  8. Message-ID: <4iokop$h4p@lyra.csx.cam.ac.uk>
  9. NNTP-Posting-Host: alf2.mrc-lmb.cam.ac.uk
  10.  
  11. Are there any limitations on what the sort function passed over to qsort can
  12. do or return? I know it's meant to return < 0, 0 or > 0 for the various
  13. compare operations, but which you return is purely up to your own comparison
  14. system.
  15.  
  16. On tracking down a bug in some old code I noticed that we had the
  17. compare function returning something like "a > b" instead of "b - a".
  18. Now this is obviously some silly bug in our coding, but "a > b" is still
  19. a valid sort function surely? The reason I ask is that this that such
  20. functions appear to make the Irix 5.3 qsort() function underflow the
  21. passed array. Please check the following function to verify that I
  22. haven't done something daft.
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. static int sort_func(const void *pa, const void *pb)
  28. {
  29.     const int *a = (int *)pa;
  30.     const int *b = (int *)pb;
  31.  
  32.     return *a > *b;
  33.  
  34. }
  35.  
  36. #define NUM_ELE 10
  37. int main() {
  38.     int i;
  39.     int crashme;     /* removing this line fixes things! */
  40.     int sortme[NUM_ELE];
  41.  
  42.     srand(time(NULL));
  43.     for (i=0; i<NUM_ELE; i++) {
  44.         sortme[i] = rand()%100+50;
  45.     }
  46.  
  47.     qsort((void *)sortme, NUM_ELE, sizeof(int), sort_func);
  48.  
  49.     return 0;
  50. }
  51.  
  52. Adding some debugging information to this and printing up the array
  53. before and after sorting (including the values (666) immediately above
  54. and below the array) shows that the contents of memory outside of the
  55. array actually get swapped with memory inside the array. I can make it
  56. overflow too. My understanding of this is that qsort() ought to be able
  57. to handle any sort function, even if it's something as dumb as
  58. (rand()%3)-1.
  59.  
  60. Anyway, my debugging output shows:
  61.  
  62. 666 < 98 61 57 50 125 73 111 103 131 136 > 666
  63. qsort it...
  64. 50 < 57 61 73 98 666 103 111 125 131 136 > 666
  65.  
  66. Anyone seen this before? I've checked SunOS4, SunOS5, DEC OSF/3.0 and
  67. Irix 5.3 and so far only found Irix to be deficient.
  68.  
  69. Bye for now,
  70.  
  71.     James
  72. --
  73. James Bonfield (jkb@mrc-lmb.cam.ac.uk)   Tel: 01223 402499   Fax: 01223 412282
  74. Medical Research Council - Laboratory of Molecular Biology,
  75. Hills Road, Cambridge, CB2 2QH, England.
  76.